package com.docMan.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
/**
 * 读取 word excel pdf txt 等可编辑文档中的内容
 * 等文件的内容
 * @author 小泽玛丽罗
 *
 */
public class PoiUtils {
	
	public  static String convert2Html(File file,String fileId) throws Exception{
		String fileType=ProjectUtils.getFileType(file.getName());
		String url_path="";
		switch (fileType) {
		case "docx":
			return getWordAfter2007(file,"\n","");
		case "doc":
			Word2Html.convert2Html(file.getAbsolutePath(),
					ProjectUtils.getProject(FinalConfig.POI_OFFICE2HTML_PATH)
							+ fileId + FinalConfig.POI_HTML,
					ProjectUtils.getProject(FinalConfig.POI_OFFICE2HTML_IMAGE_PATH));
			url_path = FinalConfig.POI_OFFICE2HTML_PATH + fileId
					+ FinalConfig.POI_HTML;
			return url_path;
			//			return getWordBefore2007(file,"\n","");
		case "txt":
			return getTxt(file);
		case "xls":
			Excel2Html.convert2Html(file.getAbsolutePath(),
					ProjectUtils.getProject(FinalConfig.POI_OFFICE2HTML_PATH)
							+ fileId + FinalConfig.POI_HTML);
			url_path=FinalConfig.POI_OFFICE2HTML_PATH + fileId
					+ FinalConfig.POI_HTML;
//			return getExcel(file) ;
			return url_path;
		case "pdf":
		default:
			return "";
		}
	}
	
	public static String  getFileContentOnlyText(File file) throws Exception{
		String fileType=ProjectUtils.getFileType(file.getName());
		switch (fileType) {
		case "docx":
			return getWordAfter2007(file,"\n","");
		case "doc":
			
			return getWordBefore2007(file,"\n","");
		case "txt":
			return getTxt(file);
		case "xls":
			return getExcel(file) ;
		case "pdf":
		default:
			return "";
		}
		
	}
	
	public static String getFileContent(File file) throws Exception{
		String fileType=ProjectUtils.getFileType(file.getName());
		switch (fileType) {
		case "docx":
			return getWordAfter2007(file,"\r\n","<br/>");
		case "doc":
			return getWordBefore2007(file,"\r\n","<br/>");
		case "txt":
			return getTxt(file);
		case "xls":
			return getExcel(file) ;
		case "pdf":
		default:
			return "";
		}
	}
	
	
	@SuppressWarnings("resource")
	private static String getExcel(File file) throws IOException{
		FileInputStream in=new FileInputStream(file);
		HSSFWorkbook wb=new HSSFWorkbook(in);
		ExcelExtractor excelExtractor=new ExcelExtractor(wb);
		excelExtractor.setFormulasNotResults(true);
		excelExtractor.setIncludeSheetNames(false);
		return excelExtractor.getText();
	}
	@SuppressWarnings("resource")
	private static String getWordBefore2007(File file,String replaceStr,String toStr) throws IOException{
		FileInputStream in=new FileInputStream(file);
		WordExtractor extractor=new WordExtractor(in);
		String text=extractor.getText().replaceAll(replaceStr, toStr);
		return text;
	}
	@SuppressWarnings("resource")
	private  static String getWordAfter2007(File file,String relaceStr,String toStr) throws IOException, XmlException, OpenXML4JException{
		OPCPackage opcPackage=POIXMLDocument.openPackage(file.getCanonicalPath());
		POIXMLTextExtractor poixmlTextExtractor=new XWPFWordExtractor(opcPackage);
		String text=poixmlTextExtractor.getText().replaceAll(relaceStr, toStr);
		return text;
	}
	
	@SuppressWarnings("resource")
	private static String getTxt(File file) throws Exception{
		StringBuilder stringBuilder=new StringBuilder();
		InputStreamReader in=new InputStreamReader(new FileInputStream(file), ProjectUtils.codeString(file.getCanonicalPath()));
		BufferedReader bufferedReader=new BufferedReader(in);
		String line="";
		while((line=bufferedReader.readLine())!=null){
			stringBuilder.append(line);
		}
		System.out.println(stringBuilder.toString());
		return stringBuilder.toString();
	}
	
	
}
